<!DOCTYPE html>

<html>
<head>
	<meta charset="utf-8" />
	<link rel="stylesheet" type="text/css" href="style.css" />
	<title>Accueil</title>

</head>
<body>

	<?php
		session_start();
	?>
		<?php

			if (isset($_GET['idSuj']))
				{
 					$idSuj = $_GET['idSuj'];
 					if (isset($_GET['page']))
 						{
 							$page = $_GET['page'];
						}
					else 
						{
							$page = 1;
						}
				}
			else 
				{
					?>

						<script type="text/JavaScript">
							document.location.href="index.php";
						</script>									
					
					<?php
				}
				?>
				
	<div class="top-menu">
		<ul>
			<li><a href="index.php" >Accueil</a></li>
			<li><a href="affCategorie.php">Catégories</a></li>
			<li><a href="#">Link 3</a></li>
			<li><a href="#">Link 4</a></li>
			<li><a href="#">Link 5</a></li>
		</ul>
		
		<?php
		if (!isset($_SESSION) || !isset($_SESSION['connected']) || !$_SESSION['connected'] == true)
		{
			?>
		<div class="login-box">
		<form action="connexion.php" method="post">
			Connexion :<input type="text" name="login" placeholder="Login" />
			<input type="text" name="mdp" placeholder="Password" />
			<input type="submit" value="OK">
		</form>
		</div>
		<?php
		}
		else
		{
			?>
			<div class="login-box">Vous etes connecté en tant que <?php
			if (isset($_SESSION) && isset($_SESSION["login"]))
				echo '<div class="login-name">' . $_SESSION["login"] . '</div>';
			?>
			<a href="sdestr.php">Déconnexion</a>
			</div>
			<?php
		}
		?>
		<br />
	</div>
	<div class="info-box-frame">
	<div class="info-box">
		<?php
			if(!isset($_GET['page']))
				{
		?>
		<?php
		if (!isset($_SESSION) || !isset($_SESSION['connected']) || !$_SESSION['connected'] == true)
		{
			?>
			<div class="info-box-title">Nouveau sur le forum ? Inscrivez vous vite !<br /></div>
		
					<form action="inscription.php?page=1" method="post">
						<input type="text" name="login" placeholder="Login" />
						<input type="text" name="mdp" placeholder="Password"/>
						<input type="text" name="email" placeholder="Mail address"/>
						<input type="text" name="nom" placeholder="Name"/>
						<input type="text" name="prenom" placeholder="Frist name"/>						
						<input type="submit" value="OK">
					</form>
				<?php
				}
				?>
		<?php
				}
			else 
				{
					$error = false;
					if (strlen($_POST['login']) < 3 || strlen($_POST['login']) > 255 || empty($_POST['login']))
						{
							$errorToAff = '<p>Le login n\'est pas conforme<p>';
							$error = true;
						}
					if (strlen($_POST['mdp']) < 3 || strlen($_POST['mdp']) > 255 || empty($_POST['mdp']))
						{
							$errorToAff .= '<p>Le mot de passe n\'est pas conforme</p>';
							$error = true;
						}
					if (!preg_match("#^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]{2,}\.[a-z]{2,4}$#", $_POST['email']) || empty($_POST['email']))
						{
							$errorToAff .= '<p>L\'email n\'est pas conforme</p>';
							$error = true;
						}
					if (empty($_POST['nom']))
						{
							$errorToAff .= '<p>Veuillez entrer un nom</p>';
							$error = true;
						}
					if (empty($_POST['prenom']))
						{
							$errorToAff .= '<p>Veuillez entrer un pr&ecute;nom</p>';
							$error = true;
						}
					if ($error)
						{
							echo $errorToAff;
							echo '</ br><a href="inscription.php">Retour a l\'inscription</a>';
						}
					else 
						{
							$mysqli = new mysqli("localhost", "root", "vlvlvl59150", "forum");

							/* Vérification de la connexion */
							if (mysqli_connect_errno())
								{
    								echo 'Échec de la connexion : ' . mysqli_error();
    								exit;
								}
							$requete = 'INSERT INTO user (login, mdp, email, nom, prenom, isAdmin, isModo) VALUES (\'' . $_POST['login'] . '\', \'' . sha1($_POST['mdp']) . '\', \'' . $_POST['email'] . '\', \'' . $_POST['nom'] . '\', \'' . $_POST['prenom'] . '\', \'0\', \'0\');';
							if ($result = $mysqli->query($requete))
								{			
									echo '</ br><a href="login.php">Vous &ecirc;tes inscrit, veuillez vous connecter maintenant.</a>';
									$result->close();
								}
							else 
								{
									echo '<p>La requ&ecirc;te a echoué</p>';
								}
							$mysqli->close();
						}
				}
		?>
	</div>
</div>
	<!-- BEGIN POSTBOX BLOCK********************************************************************************************************************** -->
	<div class="postbox">
		<div class="postbox-title">
            <?php

            ?>
            
				[Category] > [Topic] > [Post_Title] by [Post_Autor]
				<?php
		if (isset($_SESSION) && isset($_SESSION['connected']) && $_SESSION['connected'] == true && ($_SESSION['isAdmin'] || $_SESSION['isModo']))
		{
			?>
				<a href="delete_post.php">Supprimer le post</a>
		<?php
		}
		?>
			</div>
		<div class="postbox-avatar_box">
			<img src="ressources/images/NoAvatar.png" />
				<div class="postbox-bottom_bar">							

				</div>
		</div>
		<div class="postbox-frame">
			<div class="postbox-text">
			Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
			tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
			quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
			consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
			cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
			proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
			</div>
		</div>
	</div>
	<!-- END POSTBOX BLOCK********************************************************************************************************************** -->
	<footer>
			<p>
				[/!\FIXME/!\	INSERT_PHP_FOR_NB_PAGES]
			</p>
	</footer>

</body>

</html>